﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PRJ_LDW_Eutoliso.AccountAdmin.Noticia
{
    public partial class Insert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void btnInserir_Click(object sender, EventArgs e)
        {
            Gravar();
        }

        private void Gravar()
        {
            try
            {

                using (eutolisodbEntities1 context = new eutolisodbEntities1())
                {

                    lisonoticias _n = new lisonoticias();

                    if (FileUploadControl.HasFile)
                    {
                        try
                        {
                            if (FileUploadControl.PostedFile.ContentType == "~/Images/jpeg")
                            {
                                if (FileUploadControl.PostedFile.ContentLength < 102400)
                                {
                                    string filename = System.IO.Path.GetDirectoryName(FileUploadControl.FileName);
                                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                                    StatusLabel.Text = filename;
                                }
                                else
                                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                            }
                            else
                                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                        }
                        catch (Exception ex)
                        {
                            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                        }
                    }

                     string dataAtual = Convert.ToString(DateTime.Now);

                    _n.Titulo = txtTitulo.Text;
                    _n.Data = Convert.ToDateTime(dataAtual);
                    _n.Imagem1 = "~/Images/" + FileUploadControl.FileName;
                    _n.Videolink = txtVideo.Text;
                    _n.Categorias = txtCategoria.Text;
                    _n.DescricaoPq = txtDescBreve.Text;
                    _n.DescricaoGr = txtDescGrande.Text;

                    context.lisonoticias.Add(_n);
                    context.SaveChanges();

                    msgImgInserida.Visible = true;

                    Image1.ImageUrl = _n.Imagem1;
                    Image1.Width = 200;
                    Image1.Height = 200;

                    lblMsg.Visible = true;


                }


            }

            catch (Exception)
            {
                
                throw;
            }


        }

    }
}